package com.example.gitpart03;

import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;
import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import java.util.ArrayList;

public class HomeActivity extends AppCompatActivity {
    private EditText etName, etStudentId, etGender, etScore;
    private Button btnAdd, btnUpdate, btnDelete;
    private ListView lvStudents;
    private ArrayList<Student> studentList;
    private ArrayAdapter<Student> adapter;
    private int selectedPosition = -1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_home);
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.home), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });

        initViews();
        setupList();
        setupButtons();
    }

    private void initViews() {
        etName = findViewById(R.id.etName);
        etStudentId = findViewById(R.id.etStudentId);
        etGender = findViewById(R.id.etGender);
        etScore = findViewById(R.id.etScore);
        btnAdd = findViewById(R.id.btnAdd);
        btnUpdate = findViewById(R.id.btnUpdate);
        btnDelete = findViewById(R.id.btnDelete);
        lvStudents = findViewById(R.id.lvStudents);
    }

    private void setupList() {
        studentList = new ArrayList<>();
        adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, studentList);
        lvStudents.setAdapter(adapter);

        lvStudents.setOnItemClickListener((parent, view, position, id) -> {
            selectedPosition = position;
            Student student = studentList.get(position);
            etName.setText(student.getName());
            etStudentId.setText(student.getStudentId());
            etGender.setText(student.getGender());
            etScore.setText(String.valueOf(student.getScore()));
        });
    }

    private void setupButtons() {
        btnAdd.setOnClickListener(v -> addStudent());
        btnUpdate.setOnClickListener(v -> updateStudent());
        btnDelete.setOnClickListener(v -> deleteStudent());
    }

    private void addStudent() {
        String name = etName.getText().toString().trim();
        String studentId = etStudentId.getText().toString().trim();
        String gender = etGender.getText().toString().trim().toLowerCase();
        String scoreStr = etScore.getText().toString().trim();

        if(name.isEmpty() || studentId.isEmpty() || gender.isEmpty() || scoreStr.isEmpty()) {
            Toast.makeText(this, "请填写完整信息", Toast.LENGTH_SHORT).show();
            return;
        }

        if(!gender.equals("男") && !gender.equals("女") &&
                !gender.equals("male") && !gender.equals("female")) {
            Toast.makeText(this, "性别请输入男/女或male/female", Toast.LENGTH_SHORT).show();
            return;
        }

        try {
            double score = Double.parseDouble(scoreStr);
            String normalizedGender = gender.equals("male") ? "男" :
                    gender.equals("female") ? "女" : gender;

            studentList.add(new Student(name, studentId, normalizedGender, score));
            adapter.notifyDataSetChanged();
            clearFields();
            Toast.makeText(this, "添加成功", Toast.LENGTH_SHORT).show();
        } catch (NumberFormatException e) {
            Toast.makeText(this, "请输入有效的成绩", Toast.LENGTH_SHORT).show();
        }
    }

    private void updateStudent() {
        if(selectedPosition == -1) {
            Toast.makeText(this, "请先选择要修改的学生", Toast.LENGTH_SHORT).show();
            return;
        }

        String name = etName.getText().toString().trim();
        String studentId = etStudentId.getText().toString().trim();
        String gender = etGender.getText().toString().trim().toLowerCase();
        String scoreStr = etScore.getText().toString().trim();

        if(name.isEmpty() || studentId.isEmpty() || gender.isEmpty() || scoreStr.isEmpty()) {
            Toast.makeText(this, "请填写完整信息", Toast.LENGTH_SHORT).show();
            return;
        }

        if(!gender.equals("男") && !gender.equals("女") &&
                !gender.equals("male") && !gender.equals("female")) {
            Toast.makeText(this, "性别请输入男/女或male/female", Toast.LENGTH_SHORT).show();
            return;
        }

        try {
            double score = Double.parseDouble(scoreStr);
            String normalizedGender = gender.equals("male") ? "男" :
                    gender.equals("female") ? "女" : gender;

            Student student = studentList.get(selectedPosition);
            student.setName(name);
            student.setStudentId(studentId);
            student.setGender(normalizedGender);
            student.setScore(score);

            adapter.notifyDataSetChanged();
            clearFields();
            selectedPosition = -1;
            Toast.makeText(this, "更新成功", Toast.LENGTH_SHORT).show();
        } catch (NumberFormatException e) {
            Toast.makeText(this, "请输入有效的成绩", Toast.LENGTH_SHORT).show();
        }
    }

    private void deleteStudent() {
        if(selectedPosition == -1) {
            Toast.makeText(this, "请先选择要删除的学生", Toast.LENGTH_SHORT).show();
            return;
        }

        studentList.remove(selectedPosition);
        adapter.notifyDataSetChanged();
        clearFields();
        selectedPosition = -1;
        Toast.makeText(this, "删除成功", Toast.LENGTH_SHORT).show();
    }

    private void clearFields() {
        etName.setText("");
        etStudentId.setText("");
        etGender.setText("");
        etScore.setText("");
    }
}

class Student {
    private String name;
    private String studentId;
    private String gender;
    private double score;

    public Student(String name, String studentId, String gender, double score) {
        this.name = name;
        this.studentId = studentId;
        this.gender = gender;
        this.score = score;
    }

    public String getName() { return name; }
    public void setName(String name) { this.name = name; }
    public String getStudentId() { return studentId; }
    public void setStudentId(String studentId) { this.studentId = studentId; }
    public String getGender() { return gender; }
    public void setGender(String gender) { this.gender = gender; }
    public double getScore() { return score; }
    public void setScore(double score) { this.score = score; }

    @Override
    public String toString() {
        return String.format("%s (学号:%s) - %s - 成绩:%.1f", name, studentId, gender, score);
    }
}
